package com.hmall.cart.listener;

import com.hmall.cart.Service.ICartService;
import com.hmall.common.utils.UserContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.lang.annotation.ElementType;
import java.util.Collection;

@Slf4j
@RequiredArgsConstructor
@Component
public class PayDeleteCart {

    private final ICartService cartService;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "cart.clear.queue"), // 声明队列
            exchange = @Exchange(name = "trade.topic",type = ExchangeTypes.TOPIC), // 声明交换机
            key = {"order.create"} // 声明交换机key
    ))
    public void listenPaySuccess(Collection<Long> itemIds, @Header("userInfo") Long userId) {
        UserContext.setUser(userId);
        log.info("执行购物车清空操作:{}",itemIds);
        cartService.removeByItemIds(itemIds);
    }

}
